package com.brkj.util.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

public class MyScrollLayout extends ViewGroup {

	private static final String TAG = "ScrollLayout";
	private VelocityTracker mVelocityTracker; // �ㄤ��ゆ��╁����
	private static final int SNAP_VELOCITY = 600;
	private Scroller mScroller; // 婊���у�?
	private int mCurScreen;
	private int mDefaultScreen = 0;
	private float mLastMotionX;
	private OnViewChangeListener mOnViewChangeListener;
	private final static int TOTAL_SCR_COUNTS = 4;
	// 浠ヤ���釜�戒负蹇��������娉�锛��椤诲��ㄧ��规�
	public MyScrollLayout(Context context) {
		super(context);
		init(context);
	}

	public MyScrollLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public interface OnViewChangeListener {
		public void OnViewChange(int view);
	}
	
	// ������viewgroup娣诲�涓�釜scroller
	private void init(Context context) {
		mCurScreen = mDefaultScreen;
		mScroller = new Scroller(context);
	}

	// �剧ずviewgroup����?癸�蹇�』�规�
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (changed) {
			int childLeft = 0;
			final int childCount = getChildCount();
			for (int i = 0; i < childCount; i++) {
				final View childView = getChildAt(i);
				if (childView.getVisibility() != View.GONE) {
					final int childWidth = childView.getMeasuredWidth();
					childView.layout(childLeft, 0, childLeft + childWidth,
							childView.getMeasuredHeight());
					childLeft += childWidth;
				}
			}
		}
	}

	// �峰�viewgroup瀛��浠剁�瀹藉�楂�姝ゆ�娉��椤��������剧ず
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int width = MeasureSpec.getSize(widthMeasureSpec);
		final int count = getChildCount();
		for (int i = 0; i < count; i++) {
			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
		}
		// �剧ず瑙��绉诲��版�瀹��缃�?		scrollTo(mCurScreen * width, 0);
	}

	// 蹇���剁��舵�浠惰��ㄨ�姹������朵�涓���������存�瀹��mScrollX��ScrollY���������渚��灏辨��ㄤ�涓�����姝ｅ�浣跨�Scroller杩��婊���ㄧ��跺�浼���ц���?	// ��互锛��璇ユ�娉��娉ㄩ��ョ�锛�户�胯�涓��娉��璇�����浼��Scroller瀵硅薄�虹���
	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {// 褰��瑕��������缃��锛���ㄦ��芥���������rue锛�〃绀哄��昏�娌℃�缁�����缃����互���涓�釜�扮�浣�疆��
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();// �锋����
		}
	}

	// ����ゆ�璇��?
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float x = event.getX();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			Log.i("", "onTouchEvent  ACTION_DOWN");
			if (mVelocityTracker == null) {
				mVelocityTracker = VelocityTracker.obtain();
				mVelocityTracker.addMovement(event);
			}
			if (!mScroller.isFinished()) {
				mScroller.abortAnimation();// Scroller婊���版�缁�涓�浣�疆�朵腑姝㈠��汇�
			}
			mLastMotionX = x;
			break;
		case MotionEvent.ACTION_MOVE:
			int deltaX = (int) (mLastMotionX - x);
			if (IsCanMove(deltaX)) {
				if (mVelocityTracker != null) {
					mVelocityTracker.addMovement(event);
				}
				mLastMotionX = x;
				scrollBy(deltaX, 0);
			}
			break;
		case MotionEvent.ACTION_UP:
			int velocityX = 0;
			if (mVelocityTracker != null) {
				mVelocityTracker.addMovement(event);
				mVelocityTracker.computeCurrentVelocity(1000);
				velocityX = (int) mVelocityTracker.getXVelocity();
			}
			if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
				// Fling enough to move left
				Log.i(TAG, "snap left");
				snapToScreen(mCurScreen - 1);// 婊��涓��灞�
			} else if (velocityX < -SNAP_VELOCITY
					&& mCurScreen < getChildCount() - 1) {
				// Fling enough to move right
				Log.i(TAG, "snap right");
				snapToScreen(mCurScreen + 1);// 婊��涓��灞�
			} else {
//				if (velocityX < -300
//						&& mCurScreen ==TOTAL_SCR_COUNTS -1){
//					mOnViewChangeListener.OnViewChange(mCurScreen+1);
//				}
				if (mCurScreen ==TOTAL_SCR_COUNTS -1){
					mOnViewChangeListener.OnViewChange(mCurScreen+1);
				}
				snapToDestination();
			}
			if (mVelocityTracker != null) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			}
			break;
		}
		return true;
	}

	public void snapToScreen(int whichScreen) {
		// get the valid layout page
		whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
		if (getScrollX() != (whichScreen * getWidth())) {
			final int delta = whichScreen * getWidth() - getScrollX();
			mScroller.startScroll(getScrollX(), 0, delta, 0,
					Math.abs(delta) * 2);
			mCurScreen = whichScreen;
			invalidate(); // Redraw the layout
			if (mOnViewChangeListener != null) {
				mOnViewChangeListener.OnViewChange(mCurScreen);
			}
		}
	}

	public void snapToDestination() {
		final int screenWidth = getWidth();
		final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
		snapToScreen(destScreen);
	}

	private boolean IsCanMove(int deltaX) {
		if (getScrollX() <= 0 && deltaX < 0) {
			return false;
		}
		if (getScrollX() >= (getChildCount() - 1) * getWidth() && deltaX > 0) {
			return false;
		}
		return true;
	}

	public void SetOnViewChangeListener(OnViewChangeListener listener) {
		mOnViewChangeListener = listener;
	}
}
